package com.example.shopping.parsers;

import com.example.shopping.api.SHParserData;
import com.example.shopping.api.SHResponseData;
import com.example.shopping.shdatas.SHError;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by doanht on 12/5/13.
 */
public class SHErrorParserData implements SHParserData{
    public String requiredResponseType(){
        return "ErrorParser";
    }
    public SHResponseData parseData(JSONObject response) throws org.json.JSONException
    {
        SHError error = null;

        JSONObject errorJson = null;
        try {
            errorJson = response.getJSONObject("error");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (errorJson!=null){
            error = new SHError(0, "");
            try {
                error.statusCode = errorJson.getInt("code");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                error.messageCode = errorJson.getString("message");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return error;
    }
}
